// JavaScript Document
// kusht.js
// Here we have the client-side model

// This is the big important variable that holds 
// all of the client's game-state information that 
// he's allow to have access to
var client_state = {};

function updateClient() {
	look();	
}

// Found out if it's a proper client-side command
// What are client side commands? Any information
// that the character would be able to find by a simple
// lookup operation --> Any data-fetching command is local
var CMDLIST = ["look","who","equip"];
var command = {};
command['look'] = look;
command['who'] = who;
command['equip'] = equip;

function cmd_handler(msg) {
	cmdWord = msg.substring(0,msg.search(" "));
	if (cmdWord=="") { cmdWord = msg; }
	cmd = "";
	
	if(msg=="") {
		return;
	}
	
	// Reserved by regex, so bypass
	if(".?]+*$^{}[\\/".indexOf(msg[0])!==-1) { try{ socket.send(msg); } catch(ex){ log(ex); } return; }
	// Skip common server-side command abbreviations
	if (msg.length==1 && "nsewud".indexOf(msg)!==-1) { try{ socket.send(msg); } catch(ex){ log(ex); } return; }
	
	cmdWord = new RegExp(["^" + cmdWord.toLowerCase()]);
	for(i in CMDLIST) {
		if (CMDLIST[i].match(cmdWord)) {
			cmd = CMDLIST[i];
			break;
		}
	}
	
	if (cmd=="") {
		// It's probably a real system command!
		// (or junk)
		try{ socket.send(msg); } catch(ex){ log(ex); }
		return;
	}
	
	// Call the function!
	command[cmd]();
	
	//var RegExp
	//split
}

function look() {
	tmp = "";
	tmp += "<div id='room_img'><img id='room_img_act' src=images/"+client_state.in_room.image+" /></div>";
	tmp += "<div id='room_name'>"+client_state.in_room.name+"</div>";
	tmp += "<div id='room_desc'>"+client_state.in_room.description+"</div>";
	log(tmp,"expanded_info",true);
	// Exits
	tmp = "";
	tmp += "<div id='room_exit'>Obvious Exits:&nbsp;</div>";
	for(i in client_state.visible_exits) {
		tmp += "<div id='room_exit'>"+" ["+client_state.visible_exits[i].command+"] "+client_state.visible_exits[i].name+"&nbsp;</div>";
	}
	log(tmp,"exit_info",true);
	
	// -----------
	
	tmp ="";
	for(i in client_state.people_in_room) {
		tmp += "<div id='char_in_room_img'><img id='char_in_room_img_act' src=images/" + client_state.people_in_room[i].image + " /></div>";
		tmp += "<div id='char_in_room_name'>"+client_state.people_in_room[i].name+"</div>";
	}
	log(tmp,"critical_info",true);
}

function who() {
	log("<div id='general_msg'>The Following People are Online:&nbsp;</div><br class='clear'>");
	for(i in client_state.whois) {
		log("<div id='char_in_room_img'><img id='char_in_room_img_act' src=images/" + client_state.whois[i].image + " /></div>");
		log("<div id='char_in_room_name'>"+client_state.whois[i].name+"</div>");
	}
}

function equip() {
	// This works for a future implementation
	//$("equipment").setAttribute('style','z-index:1; position:absolute; left:25%; top:5%; width:50%; height:90%; min-width:600px; min-height:600px; background:black;');
}